package com.fenghuajueli.idiomppp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.fenghuajueli.idiomppp.R;
import com.fenghuajueli.idiomppp.entity.IdiomPinParentEntity;
import com.fenghuajueli.libbasecoreui.listener.OnListClickListener;


import java.util.List;

/**
 * 选择关卡数据适配器
 */
public class IdiomSelectPassAdapter extends BaseAdapter {

    private List<IdiomPinParentEntity> idiomPinEntityList;
    private Context mContext;
    private LayoutInflater mInflater;
    private int itemWidth;
    private boolean isJieLongMode = false;//是否是成语接龙模式，是的话倒置数据，从最后一关开始
    private OnListClickListener<IdiomPinParentEntity> onBaseClick;
    private int passLevel = 0;

    public IdiomSelectPassAdapter(Context context, List<IdiomPinParentEntity> idiomPinEntityList) {
        this.idiomPinEntityList = idiomPinEntityList;
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
        itemWidth = (ScreenUtils.getScreenWidth() - ConvertUtils.dp2px(132)) / 5;
    }

    public void refreshData(List<IdiomPinParentEntity> idiomPinEntityList) {
        this.idiomPinEntityList = idiomPinEntityList;
        notifyDataSetChanged();
    }

    public int getPassLevel() {
        return passLevel;
    }

    public void setPassLevel(int passLevel) {
        this.passLevel = passLevel;
    }

    public void setOnBaseClick(OnListClickListener<IdiomPinParentEntity> onBaseClick) {
        this.onBaseClick = onBaseClick;
    }

    public void setJieLongMode(boolean jieLongMode) {
        isJieLongMode = jieLongMode;
    }

    @Override
    public int getCount() {
        return idiomPinEntityList.size();
    }

    @Override
    public Object getItem(int i) {
        return idiomPinEntityList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.idiom_select_pass_item_layout, null);
            holder.idiomText = convertView.findViewById(R.id.idiomText);
            holder.itemContainer = convertView.findViewById(R.id.itemContainer);
            holder.ivLock = convertView.findViewById(R.id.ivLock);
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(itemWidth, itemWidth);
            convertView.setLayoutParams(layoutParams);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        IdiomPinParentEntity idiomPinParentEntity = idiomPinEntityList.get(position);

        if (isJieLongMode) {
            holder.idiomText.setText(String.valueOf(idiomPinParentEntity.getIndex() - 1799));
        } else {
            holder.idiomText.setText(String.valueOf(idiomPinParentEntity.getIndex() + 1));
        }
        if (idiomPinParentEntity.getIndex() <= passLevel) {
            holder.ivLock.setVisibility(View.GONE);
//            if (position < 6) {
                holder.itemContainer.setBackgroundResource(R.mipmap.aa_btn_guanqia_on1);
//            } else if (position < 13) {
//                holder.itemContainer.setBackgroundResource(R.mipmap.aa_grade_bg2);
//            } else if (position < 20) {
//                holder.itemContainer.setBackgroundResource(R.mipmap.aa_grade_bg3);
//            } else {
//                holder.itemContainer.setBackgroundResource(R.mipmap.aa_grade_bg4);
//            }
            holder.itemContainer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    if (!QuickClickUtils.isFastClick()) {
//                        return;
//                    }
                    onBaseClick.itemClick(position, idiomPinParentEntity);
                }
            });
        } else {
            holder.itemContainer.setOnClickListener(null);
            holder.ivLock.setVisibility(View.VISIBLE);
//            holder.idiomText.setVisibility(View.INVISIBLE);
            holder.itemContainer.setBackgroundResource(R.mipmap.aa_btn_guanqiao_off1);
        }
        return convertView;
    }

    final class ViewHolder {
        TextView idiomText;
        RelativeLayout itemContainer;
        ImageView ivLock;
    }

}
